{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Numbers and Integer Precision\n",
        "\n",
        "Watch the full [C# 101 video](https://www.youtube.com/watch?v=31EmPADtv4w&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=6) for this module.\n",
        "\n",
        "Like you learned in the last module, when doing math with integers, you only get integers as a result, no decimals or fractions. The numbers are `truncated`, which just means that the remainder is cut off. You can find the remainder with `%`, the remainder operator. The remainder is the left over amount from a division problem.\n",
        "> Lets try it out!"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "quotient: 3\r\n",
            "remainder: 2\r\n"
          ]
        }
      ],
      "source": [
        "int a = 7;\n",
        "int b = 4;\n",
        "int c = 3;\n",
        "int d = (a + b) / c;\n",
        "int e = (a + b) % c;\n",
        "Console.WriteLine($\"quotient: {d}\");\n",
        "Console.WriteLine($\"remainder: {e}\");"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "What is this saying? Well when you take 11 and divide it by 3, there are 3 3s that fit into 11, with two leftover, or remaining. That's why 3 is the quotient, and 2 is the remainder\n",
        "\n",
        "## Minimum and Maximum Integer Size\n",
        "\n",
        "Because of how integers are structured in coding, there is a limit to their size.\n",
        "> Find out what it is by running the following code!"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "The range of integers is -2147483648 to 2147483647\r\n"
          ]
        }
      ],
      "source": [
        "int max = int.MaxValue;\n",
        "int min = int.MinValue;\n",
        "Console.WriteLine($\"The range of integers is {min} to {max}\");"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "That's still a pretty big range!\n",
        "> But what happens if you try to go beyond?"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "An example of overflow: -2147483646\r\n"
          ]
        }
      ],
      "source": [
        "int what = max + 3;\n",
        "Console.WriteLine($\"An example of overflow: {what}\");"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "That number, which should be really big, is now close to the minimum! This is because an `overflow` \"wraps,\" going back to the minimum and then continuing to count.\n",
        "\n",
        "## Doubles: Precision and Size\n",
        "\n",
        "Doubles are another form of numbers. They can hold and answer in floating point.\n",
        "> Repeat the same code from the beginning, and see the difference a double makes."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "3.6666666666666665\r\n"
          ]
        }
      ],
      "source": [
        "double a = 7;\n",
        "double b = 4;\n",
        "double c = 3;\n",
        "double d = (a + b) / c;\n",
        "Console.WriteLine(d);"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "> Here's a more complicated expression to try:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "5.25\r\n"
          ]
        }
      ],
      "source": [
        "double a = 19;\n",
        "double b = 23;\n",
        "double c = 8;\n",
        "double d = (a + b) / c;\n",
        "Console.WriteLine(d);"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "> Find out the range of doubles:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "The range of double is -1.7976931348623157E+308 to 1.7976931348623157E+308\r\n"
          ]
        }
      ],
      "source": [
        "double max = double.MaxValue;\n",
        "double min = double.MinValue;\n",
        "Console.WriteLine($\"The range of double is {min} to {max}\");"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "That's pretty big! Much larger than integers.\n",
        "\n",
        "Of course, doubles aren't perfect. They also have rounding errors.\n",
        "> Check out this rounding:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "0.3333333333333333\r\n"
          ]
        }
      ],
      "source": [
        "double third = 1.0 / 3.0;\n",
        "Console.WriteLine(third);"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Technically, 1/3 converted to decimal should be 3 repeating infinitely, but that isn't practical in coding. It's good to be aware of though, if you're working in extremely precise variables."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Playground\n",
        "\n",
        "Play around with what you've learned! Here's some starting ideas:\n",
        "> Got any homework or projects you need to do math in? Try out using code in place of a calculator.\n",
        ">\n",
        "> The moon is 238,855 miles away from Earth. If you were using the distance to the moon in a coding problem, would you need to use integers or doubles? What if you were measuring in inches? What about the sun? It's 92,955,828 miles away.\n",
        ">\n",
        "> If a person was stuck using integers, how might they print out a division problem in a mixed fraction format?"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "dotnet_interactive": {
          "language": "csharp"
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Playground\r\n"
          ]
        }
      ],
      "source": [
        "Console.WriteLine(\"Playground\");"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Continue learning\n",
        "\n",
        "> [⏩ Next Module - Numbers and Decimals](https://ntbk.io/csharp101-notebook06)\n",
        ">\n",
        "> [⏪ Last Module - Numbers and Integer Math](https://ntbk.io/csharp101-notebook04)\n",
        ">\n",
        "> [Watch the video](https://www.youtube.com/watch?v=31EmPADtv4w&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=6)\n",
        ">\n",
        "> [Documentation: Numbers in C#](https://docs.microsoft.com/dotnet/csharp/tour-of-csharp/tutorials/numbers-in-csharp?WT.mc_id=Educationalcsharp-c9-scottha)\n",
        ">\n",
        "> [Start at the beginning: What is C#?](https://www.youtube.com/watch?v=BM4CHBmAPh4&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=1)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Other resources\n",
        "\n",
        "Here's some more places to explore:\n",
        "> [Other 101 Videos](https://dotnet.microsoft.com/learn/videos?WT.mc_id=csharpnotebook-35129-website)\n",
        ">\n",
        "> [Microsoft Learn](https://docs.microsoft.com/learn/dotnet/?WT.mc_id=csharpnotebook-35129-website)\n",
        ">\n",
        "> [C# Documentation](https://docs.microsoft.com/dotnet/csharp/?WT.mc_id=csharpnotebook-35129-website)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": ".NET (C#)",
      "language": "C#",
      "name": ".net-csharp"
    },
    "language_info": {
      "file_extension": ".cs",
      "mimetype": "text/x-csharp",
      "name": "C#",
      "pygments_lexer": "csharp",
      "version": "8.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 4
}
